package com.zhiyou.controller;

import com.zhiyou.entity.Column;
import com.zhiyou.util.ColumnUtil;
import com.zhiyou.util.DBUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author sanshi
 * @desc
 * @create 2021-09-13 21:17
 */
@WebServlet("/ColumnAdd")
public class ColumnAdd extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        String columname = req.getParameter("columname");
        String columnumb = req.getParameter("columnumb");
        String sql = "insert into news_chnl (id,name) values(?,?)";
        Object[] params = {columnumb,columname};
        int i = new DBUtil().updateInfo(sql, params);
        if (i==0){
            resp.sendRedirect("columnError.jsp");
        }else{
            List<Column> columnList = new ColumnUtil().getAllColumn();
            req.getSession().setAttribute("columnList",columnList);
            req.getSession().setAttribute("columnAdd","success");
            resp.sendRedirect("/page/ColumnManagement.jsp");
        }
    }
}
